package cn.hftoday.org;

import cn.hftoday.db.*;
import junit.framework.TestCase;
import cn.hftoday.util.*;
import cn.hftoday.core.*;

public class OrganizationTest extends TestCase {

	protected void setUp() throws Exception {
		ApplicationConfig.getInstance().load();
		ConnectionPool.getInstance().open();
		ConnectionPool.getInstance().beginTransaction();
	}

	protected void tearDown() throws Exception {
		ConnectionPool.getInstance().rollback();
		ConnectionPool.getInstance().close();
	}

	public void testGetOrganizationType() {
		//insert a organization type instance for test
		OrganizationType type = new OrganizationType();
		type.setId("c764faf7-8270-46a2-b78c-62959ee48d99");
		type.setName("abcder_type");
		type.setIcon("org2.png");
		Mapper.getInstance().insert("InsertOrganizationType", type);
		
		//test organization
		Organization org = new Organization();
		org.setOrganizationType(type);
		
		OrganizationType t = org.getOrganizationType();
		assertEquals(t.getId(), "c764faf7-8270-46a2-b78c-62959ee48d99"); 
		assertEquals(t.getName(), "abcder_type"); 
		assertEquals(org.getIcon(), "org2.png");
	}
	
	public void testGetAlias() {
		Organization org = new Organization();
		org.setAlias("aliassss");
		assertEquals(org.getAlias(), "aliassss");
	}

	public void testGetLocation() {
		Organization org = new Organization();
		org.setLocation("aliassss2");
		assertEquals(org.getLocation(), "aliassss2");
	}
	
	public void testGetCompany() {
		//insert a company instance for test
		Company c = new Company();
		c.setId("c764faf7-8270-46a2-b78c-62959ee48d95");
		c.setName("abcde");
		Mapper.getInstance().insert("InsertCompany", c);
		
		//test organization
		Organization org = new Organization();
		org.setCompany(c);
		
		Company c2 = org.getCompany();
		assertEquals(c2.getId(), "c764faf7-8270-46a2-b78c-62959ee48d95"); 
		assertEquals(c2.getName(), "abcde"); 
	}
	
	public void testSelect() {
		testInsert();
		
		Organization org = (Organization)Mapper.getInstance().queryForObject("SelectOrganizationById", "23456788900opqmm");
		assertEquals(org.getName(), "huitailang");
		assertEquals(org.isHidden(), false);
		assertEquals(org.getCreateTime(), CalendarParser.toCalendar("2009-01-14 00:00:00"));
		assertEquals(org.getUpdateTime(), CalendarParser.toCalendar("2009-01-15 00:00:00"));
		assertEquals(org.getAlias(), "xiyangyang");
		assertEquals(org.getLocation(), "tvb");
	}

	public void testInsert() {
		Organization org = new Organization();
		org.setId("23456788900opqmm");
		org.setName("huitailang");
		org.setHidden(false);
		org.setCreateTime(CalendarParser.toCalendar("2009-01-14 00:00:00"));
		org.setUpdateTime(CalendarParser.toCalendar("2009-01-15 00:00:00"));
		org.setAlias("xiyangyang");
		org.setLocation("tvb");
		Object o = Mapper.getInstance().insert("InsertOrganization", org);
		assertSame(org, o);
	}
	
	public void testUpdate() {
		testInsert();
		Organization org = (Organization)Mapper.getInstance().queryForObject("SelectOrganizationById", "23456788900opqmm");
		org.setName("huitailang2");
		org.setHidden(true);
		org.setCreateTime(CalendarParser.toCalendar("2009-01-14 00:00:10"));
		org.setUpdateTime(CalendarParser.toCalendar("2009-01-15 00:00:20"));
		org.setAlias("xiyangyang2");
		org.setLocation("tvbs");
		
		Organization org2 = (Organization)Mapper.getInstance().queryForObject("SelectOrganizationById", "23456788900opqmm");
		assertEquals(org.getName(), "huitailang2");
		assertEquals(org.isHidden(), true);
		assertEquals(org.getCreateTime(), CalendarParser.toCalendar("2009-01-14 00:00:10"));
		assertEquals(org.getUpdateTime(), CalendarParser.toCalendar("2009-01-15 00:00:20"));
		assertEquals(org.getAlias(), "xiyangyang2");
		assertEquals(org.getLocation(), "tvbs");
	}
	
	public void testDelete() {
		testInsert();
		Organization org = (Organization)Mapper.getInstance().queryForObject("SelectOrganizationById", "23456788900opqmm");
		Mapper.getInstance().delete("DeleteOrganization", org);
		Organization org2 = (Organization)Mapper.getInstance().queryForObject("SelectOrganizationById", "23456788900opqmm");
		assertEquals(org2, null);
	}
}
